package com.sjgtw.cloud.web.waterDamage.controller;

import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import com.sjgtw.cloud.web.waterDamage.service.IWaterDamageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 水害台账Controller
 * 
 * @author lvyoufang
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/waterDamage")
@Api(tags = "4.2、水害台账")
public class WaterDamageController extends BaseController
{
    @Autowired
    private IWaterDamageService waterDamageService;

    /**
     * 查询水害台账列表
     */
    @ApiOperation(value = "查询水害台账列表")
    @GetMapping("/list")
    public TableDataInfo list(WaterDamage waterDamage)
    {
        startPage();
        List<WaterDamage> list = waterDamageService.selectWaterDamageList(waterDamage);
        return getDataTable(list);
    }

    /**
     * 获取水害台账详细信息
     */
    @ApiOperation(value = "获取水害台账详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(waterDamageService.selectWaterDamageById(id));
    }

    /**
     * 新增水害台账
     */
    @ApiOperation(value = "新增水害台账")
    @PostMapping
    public AjaxResult add(@RequestBody WaterDamage waterDamage)
    {
        return toAjax(waterDamageService.insertWaterDamage(waterDamage));
    }

    /**
     * 修改水害台账
     */
    @ApiOperation(value = "修改水害台账")
    @PutMapping
    public AjaxResult edit(@RequestBody WaterDamage waterDamage)
    {
        return toAjax(waterDamageService.updateWaterDamage(waterDamage));
    }

    /**
     * 删除水害台账
     */
    @ApiOperation(value = "删除水害台账")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(waterDamageService.deleteWaterDamageByIds(ids));
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterDamage waterDamage)
    {
        List<WaterDamage> list = waterDamageService.selectWaterDamageList(waterDamage);
        for (WaterDamage entity : list) {
            if (entity.getImpactedDriving() != null) {
                entity.setImpactedDrivingStr(entity.getImpactedDriving() == 0 ? "否" : "是");
            }
        }
        EasyExcelUtil<WaterDamage> util = new EasyExcelUtil<>(WaterDamage.class);
        util.exportExcel(response, list, "水害台账", "水害台账", "waterDamage.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<WaterDamage> util = new EasyExcelUtil<>(WaterDamage.class);
        util.exportExcel(response, new ArrayList<>(), "水害台账", "水害台账模版", "waterDamage.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<WaterDamage> util = new EasyExcelUtil<>(WaterDamage.class);
        List<WaterDamage> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = waterDamageService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }
        for (WaterDamage entity : entities) {
            if (StringUtils.isNotEmpty(entity.getImpactedDrivingStr())) {
                if ("是".equals(entity.getImpactedDrivingStr())) {
                    entity.setImpactedDriving(1);
                } else if ("否".equals(entity.getImpactedDrivingStr())) {
                    entity.setImpactedDriving(0);
                }
            }
            entity.setSubmited(true);
        }
        waterDamageService.batchSave(entities);
        return success();
    }


}
